﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CardMonitor.Utils
{
    public class CardUtil
    {
        public static String getCardGUID()
        {
            String result = "";

            String uuid = Guid.NewGuid().ToString();

            uuid = uuid.Replace("-", "").ToUpper();

            Console.WriteLine(uuid.Length);
            return uuid;
        }

        public static String getCardGUIDCode(String code)
        {
            String result = "FFCC017A383130363138";

            for (int i = 0; i < code.Length; i++)
            {
                String charStr = code.Substring(i, 1);
                byte[] array = System.Text.Encoding.ASCII.GetBytes(charStr);
                String hexAscii = ByteUtils.byteToHexStr(array);
                //Console.WriteLine(charStr + "  " + hexAscii);
                result += hexAscii;
            }
            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getCarNumCode(String carNum, String prodCode, String num)
        {
            //车号、油品代码、预装量
            String result = "FF CC 01 7B 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            string province = carNum.Substring(0, 1);
            result += encodeProvince(province);

            String carNum2 = carNum.Substring(1, 6);
            String carNumHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(carNum2));
            result += carNumHexAscii;

            if (prodCode.Length == 1)
            {
                prodCode = "0" + prodCode;
            }
            String prodCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(prodCode));
            result += prodCodeHexAscii;

            try
            {
                num = num.PadLeft(5, '0');
            }
            catch (ArgumentOutOfRangeException ex)
            {
                num = "00000";
            }
            String numCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(num));
            result += numCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getCarNumCode(String carNum, String num)
        {
            //车号、油品代码、预装量
            String result = "FF CC 01 7B 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            string province = carNum.Substring(0, 1);
            result += encodeProvince(province);

            String carNum2 = carNum.Substring(1, 6);
            String carNumHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(carNum2));
            result += carNumHexAscii;

          /*  if (prodCode.Length == 1)
            {
                prodCode = "0" + prodCode;
            }
            String prodCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(prodCode));
            result += prodCodeHexAscii;
*/
            try
            {
                num = num.PadLeft(5, '0');
            }
            catch (ArgumentOutOfRangeException ex)
            {
                num = "00000";
            }
            String numCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(num));
            result += numCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getIcCode(String code)
        {
            String result = "FF CC 01 71 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            String icCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(code));
            result += icCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static String getBillCode(String code)
        {
            String result = "FF CC 01 79 38  31 30 36 31 38";
            result = result.Replace(" ", "");

            String billCodeHexAscii = ByteUtils.byteToHexStr(System.Text.Encoding.ASCII.GetBytes(code));
            result += billCodeHexAscii;

            result = result + ByteUtils.CalcXor(result);

            return result;
        }

        public static string encodeProvince(string province)
        {
            switch (province)
            {
                case "冀": return "313430";
                case "豫": return "313431";
                case "云": return "313432";
                case "辽": return "313433";
                case "黑": return "313434";
                case "湘": return "313435";
                case "皖": return "313436";
                case "鲁": return "313437";
                case "新": return "313438";
                case "苏": return "313439";
                case "浙": return "31343A";
                case "赣": return "31343B";
                case "鄂": return "31343C";
                case "桂": return "31343D";
                case "甘": return "31343E";
                case "晋": return "31343F";
                case "蒙": return "313440";
                case "陕": return "313441";
                case "吉": return "313442";
                case "闽": return "313443";
                case "粤": return "313444";
                case "川": return "313445";
                case "青": return "313446";
                case "藏": return "313447";
                case "琼": return "313448";
                case "宁": return "313449";
                case "渝": return "31344A";
                case "京": return "31344B";
                case "津": return "31344C";
                case "沪": return "31344D";
                case "贵": return "31344E";

            }
            return "000000";
        }
        public static string decodeProvince(string province)
        {
            switch (province)
            {
                case "313430": return "冀";
                case "313431": return "豫";
                case "313432": return "云";
                case "313433": return "辽";
                case "313434": return "黑";
                case "313435": return "湘";
                case "313436": return "皖";
                case "313437": return "鲁";
                case "313438": return "新";
                case "313439": return "苏";
                case "31343A": return "浙";
                case "31343B": return "赣";
                case "31343C": return "鄂";
                case "31343D": return "桂";
                case "31343E": return "甘";
                case "31343F": return "晋";
                case "313440": return "蒙";
                case "313441": return "陕";
                case "313442": return "吉";
                case "313443": return "闽";
                case "313444": return "粤";
                case "313445": return "川";
                case "313446": return "青";
                case "313447": return "藏";
                case "313448": return "琼";
                case "313449": return "宁";
                case "31344A": return "渝";
                case "31344B": return "京";
                case "31344C": return "津";
                case "31344D": return "沪";
                case "31344E": return "贵";
            }
            return "无";
        }
        public static string ByteArrayToHexString(byte[] dataHex, int start, int len)
        {
            if (start >= dataHex.Length || (start + len) > dataHex.Length)
            {
                throw new Exception("The index or length position is not within string.");
            }
            string str = "";
            for (int i = start; i < start + len; i++)
            {
                try
                {
                    str += dataHex[i].ToString("X2");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return str;
        }
    }
}
